package com.hhwy.controller;

import com.hhwy.HslCommunication.Core.Types.OperateResult;
import com.hhwy.HslCommunication.Profinet.Siemens.SiemensPLCS;
import com.hhwy.HslCommunication.Profinet.Siemens.SiemensS7Net;
import com.hhwy.controller.utils.Result;
import com.hhwy.domain.CableConveyorsSub;
import com.hhwy.domain.CableConveyorsSubConfig;
import com.hhwy.service.ICableConveyorsSubConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ccys/sub/control")
@Slf4j
public class CableConveyorsSubController {

    @Value("${cable-conveyors-socket.ip}")
    private String ip;
    @Autowired
    private ICableConveyorsSubConfigService cableConveyorsSubConfigService;

//    /**
//     * 向前
//     * @return
//     */
//    @GetMapping("/forward/{id}")
//    public Result forward(@PathVariable Integer id){
//        try{
//            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
//            OperateResult connect = siemens_net.ConnectServer();
//            CableConveyorsSubConfig config=cableConveyorsSubConfigService.getById(id);
//            if(connect.IsSuccess){
//                log.info("/forward connect success!");
//            }
//            else {
//                log.error("failed:"+connect.Message);
//                return new Result(0,connect.Message);
//            }
//            siemens_net.ConnectClose();
//            siemens_net.Write(config.getForwardOrBackoff(),true);
//            return new Result(1,"调用成功");
//        }catch (Exception e){
//            return new Result(0,"调用失败:"+e.getMessage());
//        }
//    }
//
//    /**
//     * 向后
//     * @return
//     */
//    @GetMapping("/backoff/{id}")
//    public Result backoff(@PathVariable Integer id){
//        try{
//            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
//            OperateResult connect = siemens_net.ConnectServer();
//            CableConveyorsSubConfig config=cableConveyorsSubConfigService.getById(id);
//            if(connect.IsSuccess){
//                log.info("/backoff connect success!");
//            }
//            else {
//                log.error("failed:"+connect.Message);
//                return new Result(0,connect.Message);
//            }
//            siemens_net.ConnectClose();
//            siemens_net.Write(config.getForwardOrBackoff(),false);
//            return new Result(1,"调用成功");
//        }catch (Exception e){
//            return new Result(0,"调用失败:"+e.getMessage());
//        }
//    }

    /**
     * 加速度
     * @return
     */
    @GetMapping("/addSpeed/{id}")
    public Result addSpeed(@PathVariable Integer id){
        try{
            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
            OperateResult connect = siemens_net.ConnectServer();
            CableConveyorsSubConfig config=cableConveyorsSubConfigService.getById(id);
            if(connect.IsSuccess){
                log.info("/backoff connect success!");
            }
            else {
                log.error("failed:"+connect.Message);
                return new Result(0,connect.Message);
            }
            siemens_net.ConnectClose();
            siemens_net.Write(config.getSpeed0(),true);
            siemens_net.Write(config.getSpeed0(),false);
            return new Result(1,"调用成功");
        }catch (Exception e){
            return new Result(0,"调用失败:"+e.getMessage());
        }
    }

    /**
     * 减速度
     * @return
     */
    @GetMapping("/deceSpeed/{id}")
    public Result deceSpeed(@PathVariable Integer id){
        try{
            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
            OperateResult connect = siemens_net.ConnectServer();
            CableConveyorsSubConfig config=cableConveyorsSubConfigService.getById(id);
            if(connect.IsSuccess){
                log.info("/backoff connect success!");
            }
            else {
                log.error("failed:"+connect.Message);
                return new Result(0,connect.Message);
            }
            siemens_net.ConnectClose();
            siemens_net.Write(config.getSpeed1(),true);
            siemens_net.Write(config.getSpeed1(),false);
            return new Result(1,"调用成功");
        }catch (Exception e){
            return new Result(0,"调用失败:"+e.getMessage());
        }
    }

//    /**
//     * 停止
//     * @return
//     */
//    @GetMapping("/stop/{id}")
//    public Result stop(@PathVariable Integer id){
//        try{
//            SiemensS7Net siemens_net = new SiemensS7Net(SiemensPLCS.S1200,ip);
//            OperateResult connect = siemens_net.ConnectServer();
//            CableConveyorsSubConfig config=cableConveyorsSubConfigService.getById(id);
//            if(connect.IsSuccess){
//                log.info("/backoff connect success!");
//            }
//            else {
//                log.error("failed:"+connect.Message);
//                return new Result(0,connect.Message);
//            }
//            siemens_net.ConnectClose();
//            siemens_net.Write(config.getEmergencyStop(),true);
//            siemens_net.Write(config.getEmergencyStop(),false);
//            return new Result(1,"调用成功");
//        }catch (Exception e){
//            return new Result(0,"调用失败:"+e.getMessage());
//        }
//    }






}
